#pragma once
#include <map>
#include "stdafx.h"
#include "IOrderService.h"
#include "ITradeAdapter.h"
#include "threadlock.h"
#include "CTPHelper.h"

class CThostFtdcTraderApi ;
struct CThostFtdcDepthMarketDataField;

namespace tradeadapter
{
	namespace ctp
	{
		class CCTPMsgHandler;

		class CTPAPI CCTPExchangeAdapter :public ITradeAdapter,public IQuoteService
		{
		public:
			CCTPExchangeAdapter(void);
			~CCTPExchangeAdapter(void);

			virtual bool Initialize( void* param = NULL );

			virtual bool IsInitialized();

			virtual bool InitializeByXML( const TCHAR* fpath );

			virtual int EntrustOrder( Order order );

			virtual bool CancelOrder( OrderID orderid );

			virtual int QueryOrder( OrderID id );

			virtual bool QueryDealedOrders( std::list<DealedOrder>* pres );

			virtual void UnInitialize();

			virtual bool QueryAccountInfo( BasicAccountInfo* info );

			void _GetAccountName( BasicAccountInfo* info );

			virtual bool QueryPostions( std::list<PositionInfo>* pPoss,const TCHAR* pstock = NULL );

			virtual bool GetLastError( std::pair<int,tstring>* pError );

			CCTPData* GetCTPData();

			virtual bool GetQuote( const TCHAR* id,Quote* pQuote );

			const int InvalideOrderId ;//= -1;

			std::string GetCurrentIF();

			bool _SetLastError(int code,const char* msg);


			void GetContractInfo();

			bool _SetAccountInfo(CThostFtdcTradingAccountField* pInfo);

#pragma warning(disable : 4251)
			//std::map<int,std::string> m_orid2sysid;
			//std::map<int,int> m_reqid2orderid;

		private:
			bool GetDepMarket(const char* code,CThostFtdcDepthMarketDataField* pdata );
			void internalUnInitialize();

			virtual bool QueryEntrustInfos( std::list<EntrustInfo>* pinfo );

			
			
#pragma warning(disable : 4251)
			threadlock::multi_threaded_local m_lock;
			CThostFtdcTraderApi* m_api;
			//std::map<int,std::string> m_orid2strid;
			CCTPMsgHandler * m_handler;
			

			CTPParamA m_param;

			CCTPData m_ctpinfo;
			bool m_initialized;

			std::pair<int ,tstring> m_lastErr;


			tstring accontname;

		};

	};
};